package com.boss.fileapp.dao.impl;


import com.boss.fileapp.consts.TableName;
import com.boss.fileapp.dao.ISalaryBankPayBackDao;
import com.boss.fileapp.entity.CounterOffer;
import com.boss.fileapp.entity.Offer;
import com.boss.framework.dao.BaseDaoImpl;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.*;

@Repository
public class SalaryBankPayBackDaoImpl extends BaseDaoImpl implements ISalaryBankPayBackDao {

    @Override
    public void batchReplaceCounterOffer(List<CounterOffer> list,Map<String, Map<String,Object>> messageMap) {
        Map<String,Object> personIdMap=messageMap.get("personIdMap");
        Map<String,Object> personAgencyIdMap=messageMap.get("personAgencyIdMap");
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sb.append(" replace into ")
                .append(TableName.SALARY_BANK_PAY_BACK)
                .append(" (id, month_id, biz_key, update_time, create_time, iden_no, person_name, old_sala_card_no, bank_back_code, ")
                .append(" bank_back_remark, is_update, is_recreate, is_update_person, is_send, mof_div_code,person_id,agency_id) ")
                .append(" values ");
        for(CounterOffer counterOffer : list){
            sb.append(" (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,?,?) ");
            if(list.indexOf(counterOffer) != list.size() - 1){
                sb.append(" , ");
            }
            Object[] paramArr = {
                    counterOffer.getId().trim(),
                    counterOffer.getMonthId().trim(),
                    counterOffer.getBizKey().trim(),
                    counterOffer.getUpdateTime().trim(),
                    counterOffer.getCreateTime().trim(),
                    counterOffer.getIdenNo().trim(),
                    counterOffer.getPersonName().trim(),
                    counterOffer.getOldSalaCardNo().trim(),
                    counterOffer.getBankBackCode().trim(),
                    counterOffer.getBankBackRemark().trim(),
                    counterOffer.getIsUpdate(),
                    counterOffer.getIsRecreate(),
                    counterOffer.getIsUpdatePerson(),
                    counterOffer.getIsSend(),
                    counterOffer.getMofDivCode(),
                    personIdMap.get(counterOffer.getIdenNo().trim()),//20210508新增人员id
                    personAgencyIdMap.get(counterOffer.getIdenNo().trim())//20210508新增人员agencyId

            };
            params.addAll(Arrays.asList(paramArr));
        }
        super.getjdbcTemplate().update(sb.toString(), params.toArray());
    }

    /**
     * 异常卡号汇总回盘
     * @param personIds
     * @param province
     * @return
     */
    @Override
    public List<Offer> selectHZOffer(List<String> personIds, String province) {
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sb.append(" select ")
                .append(" concat(avy.salary_month, sbpbk.new_iden_no) id, ")
                .append("  concat( avy.salary_month,sbpbk.person_id ) bizKey, sbpbk.new_iden_no idenNo, sbpbk.new_person_name personName, ")
                .append(" smed.s1 realSalary, sbpbk.new_sala_card_no salaCardNo, sbpbk.new_salary_bank salaCardBank, avy.agency_code agencyCode, ")
                .append(" avy.agency_name agencyName ,avy.salary_month salaryMonth")
                .append(" from ")
                .append(TableName.SALARY_BANK_PAY_BACK).append(" sbpbk ")
                .append(" join ").append(TableName.SALARY_MONTH_END_SUBMIT).append(" smed ")
                .append(" on ").append(" smed.person_id = sbpbk.person_id  ")
                .append(" join ").append(TableName.AGENCY_VERIFY).append(" avy ")
                .append(" on ").append(" avy.agency_id = smed.agency_id and avy.is_deleted = 0 and is_month_end = 0 ")
                .append(" and avy.is_confirm_submit = 1 and is_pay_data_created = 1 and is_bank_data_created = 1 ")

                .append(" where ").append(" sbpbk.is_update = 1 and sbpbk.is_recreate = 0 and sbpbk.verified_code = 2  and sbpbk.mof_div_code = ? ");
        params.add(province);
        sb.append(" and ").append(SysUtils.inSql(" smed.person_id ",personIds));

        return super.getjdbcTemplate().query(sb.toString(), params.toArray(), new BeanPropertyRowMapper<>(Offer.class));
    }

    @Override
    public List<CounterOffer> selectBankPayBack(String province) {
        StringBuilder sb = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sb.append(" select ")
                .append(" id, month_id, biz_key, update_time, create_time, iden_no, person_name, old_sala_card_no, bank_back_code, ")
                .append(" bank_back_remark, is_update, is_recreate, is_update_person, is_send, mof_div_code ")
                .append("from  ").append(TableName.SALARY_BANK_PAY_BACK).append(" where mof_div_code=?");
        params.add(province);
        return   super.getjdbcTemplate().query(sb.toString(), params.toArray(), new BeanPropertyRowMapper<>(CounterOffer.class));
    }
}
